package com.example.h5adnative;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Field;

import cn.xll.nativechannel.MainActivity;


public class MainWebActivity extends MainActivity {

    @Override
    protected void onStart() {
        super.onStart();
        a.setWebChromeClient(new MyWebChromeClient());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

    final class MyWebChromeClient extends WebChromeClient {
        /**
         * 处理alert弹出框
         */
        @Override
        public boolean onJsAlert(WebView view, String url,
                                 String message, JsResult result) {
            Log.e("ly_ad_xxx", message);
            //对alert的简单封装
            new AlertDialog.Builder(MainWebActivity.this).
                    setTitle("万必联").setMessage(message).setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    //TODO
                                }
                            }).create().show();
            result.confirm();
            return true;
        }

        /**
         * 处理confirm弹出框
         */
        @Override
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
            Log.e("ly_ad_xxx", message);

            AlertDialog.Builder builder = new AlertDialog.Builder(MainWebActivity.this);
            builder.setTitle("万必联");
            builder.setMessage(message);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 用户点击了确定按钮，在这里执行你的操作
                    result.confirm();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 用户点击了取消按钮，在这里执行你的操作
                    result.cancel();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
            return true;
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
//            textView.setText("Prompt input is :"+message);
            Log.e("ly_ad_xxx", message);
            result.confirm();
            return super.onJsPrompt(view, url, message, message, result);
        }
    }

}
